﻿using Asmodat.Abbreviate;
using Asmodat.Extensions.Collections.Generic;
using Asmodat.Extensions.Objects;
using Asmodat.Extensions.Windows.Forms;
using Asmodat.IO;
using Asmodat.Types;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Asmodat_ELM
{
    public partial class Form1 : Form
    {
        ThreadedTimers Timers = new ThreadedTimers();
        ThreadedMethod Methods = new ThreadedMethod();

        Asmodat.IO.SerialPort _serialPort = new Asmodat.IO.SerialPort();

        public Form1()
        {
            InitializeComponent();

            

            Timers.Run(() => Peacemaker_SerialPort(), 1);
            Timers.Run(() => Peacemaker_FilesManager(), 100);

            this.TRBtnSelectChange.CheckedChanged += this.TRBtnSelectChange_CheckedChanged;
            this.TRBtnSelectSelected.CheckedChanged += this.TRBtnSelectSelected_CheckedChanged;
            this.TRBtnSelectAll.CheckedChanged += this.TRBtnSelectAll_CheckedChanged;

            TTSBtnConnection.OnClick += TTSBtnConnection_OnClick;
            TBtnUpload.OnClick += TBtnUpload_OnClick;
            TCLBxFiles.ItemCheck += TCLBxFiles_ItemCheck;
            TCbxBaud.SelectedValueChanged += TCbxBaud_SelectedValueChanged;
            TCbxPort.SelectedValueChanged += TCbxPort_SelectedValueChanged;
            TTbxSearchMask.TextChanged += TTbxSearchMask_TextChanged;

            TTbxSubmit.OnThreadedEnterKeyDown += TTbxSubmit_OnThreadedEnterKeyDown;
            TTbxSubmit.EnableKeyControl = true;
            TCbxBaud.AddItems(Asmodat.IO.SerialPort.bauds);
            TCbxBaud.SelectedItem = 115200;

            TTbxUploadTimeout.SetIntegerMode(90, 60000, 10000);

            TDgbDictionary.OnCellClick += TDgbDictionary_OnCellClick;
            TDgbDictionary.SelectionChanged += TDgbDictionary_SelectionChanged;
            TTbxUploadTimeout.TextChanged += TTbxUploadTimeout_TextChanged;

            TTbxDictionary.TextChanged += TTbxDictionary_TextChanged;

            this.LoadState();
            this.UpdateDictionary();
        }

        private Control _Invoker = null;
        public Control Invoker
        {
            get
            {
                if (_Invoker == null)
                    _Invoker = this.GetFirstParent();

                return _Invoker;
            }
        }



        private void TTbxUploadWakeup_TextChanged(object sender, EventArgs e)
        {
            SaveState_Settings();
        }

        private void TTbxUploadTimeout_TextChanged(object sender, EventArgs e)
        {
            SaveState_Settings();
        }

       

        
     
        private void TTSBtnConnection_OnClick(object source, Asmodat.FormsControls.ThreadedTwoStateButtonClickStatesEventArgs e)
        {
            if (e.On)
            {
                _serialPort.Start(TCbxPort.Text, TCbxBaud.Text.TryParse(0));
                this.Info = string.Format("Serial Port Started - Port: {0}, Baud: {1}", _serialPort.PortName, _serialPort.BaudRate);
                TCbxBaud.Enabled = TCbxPort.Enabled = false;
            }
            else
            {
                _serialPort.Stop();
                this.Info = "Serial Port Stopped";
                TCbxBaud.Enabled = TCbxPort.Enabled = true;
            }

            TTbxSubmit.Enabled = e.On;
            TBtnSend.Enabled = e.On;

            TTSBtnConnection.Switch();
        }


        
        List<string> SelectedFiles = new List<string>();

        bool TCLBxFiles_ItemCheck_Igonre { get; set; } = false;
        private void TCLBxFiles_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (TCLBxFiles_ItemCheck_Igonre) return;

            if (SelectMode == SelectModes.Changes)
            {
                string file = TCLBxFiles.Items[e.Index].ToString();

                if (e.NewValue == CheckState.Checked)
                    UploadedFiles.Remove(file);
                else
                    UploadedFiles.Add(file, TickTime.Now);

            }
            else if (SelectMode == SelectModes.Selected)
            {
                string file = TCLBxFiles.Items[e.Index].ToString();

                if (e.NewValue == CheckState.Checked)
                    SelectedFiles.AddDistinct(file);
                else
                    SelectedFiles.Remove(file);

                SaveState_FileManager();
            }
        }


        public string Info
        {
            get
            {
                return Invoker.TryInvokeMethodFunction(() => { return TSSLblInfo.Text; });
            }
            set
            {
                Invoker.TryInvokeMethodAction(() => { TSSLblInfo.Text = value; });
            }
        }
    }
}
